//
//  YWKLineCheatsClassCell.m
//  YueshengToutiao
//
//  Created by jun peng on 2022/1/4.
//  Copyright © 2022 YW. All rights reserved.
//

#import "YWKLineCheatsClassCell.h"


@interface YWKLineCheatsClassCell ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *haveReadLabel;
@property (weak, nonatomic) IBOutlet UILabel *havePraisedLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *learnedBtnConstraintWidth;
@property (weak, nonatomic) IBOutlet UIButton *learnedBtn;
@property (weak, nonatomic) IBOutlet UIButton *downloadBtn;
- (IBAction)downloadBtnClick:(UIButton *)sender;

@end


@implementation YWKLineCheatsClassCell

-(void)setStatus:(YWKLineCheatsVideoListDataStatus *)status{
    _status = status;
    
    if (status.isNeedHideRedaAndPraiseLabel == YES) {
        self.haveReadLabel.hidden = YES;
        self.havePraisedLabel.hidden = YES;
    }
    else{
        self.haveReadLabel.hidden = NO;
        self.havePraisedLabel.hidden = NO;
    }
    
    
    if (![status.PicUrl isKindOfClass:[NSNull class]] && status.PicUrl != nil) {
        
        [self.iconView sd_setImageWithURL:[NSURL URLWithString:status.PicUrl] placeholderImage:[UIImage imageNamed:@""]];
        
    }
    else{
        
        [self.iconView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""]];
        
    }
    
    self.titleLabel.text = status.Title;
    
    
    if (status.isSelect) {
        self.titleLabel.textColor = [UIColor orangeColor];
    }else{
            self.titleLabel.textColor = [UIColor blackColor];
    }
    
    NSString *ReadTimes = status.PlayTimes;
    NSInteger ReadTimesInt = ReadTimes.intValue;
    if (ReadTimesInt > 10000) {
        ReadTimesInt = ReadTimesInt/10000;
        ReadTimes = [NSString stringWithFormat:@"%ld+w",ReadTimesInt];
    }


    self.haveReadLabel.text = [NSString stringWithFormat:@"%@已阅读",ReadTimes];
    
    
    NSString *Likes = status.Likes;
    NSInteger LikesInt = Likes.intValue;
    if (LikesInt > 10000) {
        LikesInt = LikesInt/10000;
        Likes = [NSString stringWithFormat:@"%ld+w",LikesInt];
    }
    
    
    self.havePraisedLabel.text = [NSString stringWithFormat:@"%@点赞",Likes];
    
    if (status.IsLearn.intValue == 1) {
        self.learnedBtn.hidden = NO;
        self.learnedBtnConstraintWidth.constant = 60;
    }
    else{
        self.learnedBtn.hidden = YES;
        self.learnedBtnConstraintWidth.constant = 0;
    }
    
    if (status.kLineCheatsVideoListType == 0) { //等于0的话就是还没有赋值的、就要去赋值
        YWDownloadStatus3 *downloadStatus = [YWDownloadDataBaseTool getFinishStatusWithVideoUrl:status.VideoUrl];
        
        if (downloadStatus == nil) {
            
            downloadStatus = [YWDownloadDataBaseTool getDownloadingStatusWithVideoUrl:status.VideoUrl];
            
            
            if (downloadStatus == nil) {    //未加入下载的
                
                status.kLineCheatsVideoListType = YWKLineCheatsVideoList_NoDownload3;
                
            }
            else{   //在正在任务里面的
                
                status.kLineCheatsVideoListType = YWKLineCheatsVideoList_Downloading3;
            }
          
        }
        else{   //在完成的里面
            
            status.kLineCheatsVideoListType = YWKLineCheatsVideoListD_Finish3;
            
        }
    }
    
    //处理按钮
    switch (status.kLineCheatsVideoListType) {
        case YWKLineCheatsVideoList_NoDownload3:
        {
            [self.downloadBtn setTitle:@"下载" forState:UIControlStateNormal];
            [self.downloadBtn setTitleColor:YWColor(255, 92, 0) forState:UIControlStateNormal];
            self.downloadBtn.enabled = YES;
        }
            break;
        case YWKLineCheatsVideoList_Downloading3:
        {
            [self.downloadBtn setTitle:@"下载中" forState:UIControlStateNormal];
            [self.downloadBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            self.downloadBtn.enabled = NO;
        }
            break;
        case YWKLineCheatsVideoListD_Finish3:
        {
            [self.downloadBtn setTitle:@"已下载" forState:UIControlStateNormal];
            [self.downloadBtn setTitleColor:[UIColor systemOrangeColor] forState:UIControlStateNormal];
            self.downloadBtn.enabled = NO;
            
        }
            break;
            
        default:
            break;
    }
  
    
}


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"YWKLineCheatsClassCell";
    YWKLineCheatsClassCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        // 从xib中加载cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YWKLineCheatsClassCell" owner:nil options:nil] lastObject];
    }
    return cell;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.learnedBtn.layer.cornerRadius = 12;
    self.downloadBtn.layer.cornerRadius = 12;
    self.iconView.layer.cornerRadius = 5;
    self.iconView.clipsToBounds = YES;
    
    self.learnedBtn.userInteractionEnabled = NO;
    
    
    //监听通知改变KLineCheatsVideoListCell状态
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKLineCheatsVideoListTypeNote:) name:YWChangeKLineCheatsVideoListTypeNote object:nil];
}

-(void)changeKLineCheatsVideoListTypeNote:(NSNotification *)notification{
    
    NSString *videoUrl = notification.userInfo[YWVideoUrlKey];
    NSNumber *kLineCheatsVideoListTypeNum = notification.userInfo[YWKLineCheatsVideoListTypeKey];
    
    if ([self.status.VideoUrl isEqualToString:videoUrl]) {
        self.status.kLineCheatsVideoListType = kLineCheatsVideoListTypeNum.integerValue;
        self.status = self.status;
    }
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
}

- (IBAction)downloadBtnClick:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(lineCheatsClassCellClickDownload:withStatus:)]) {
        [self.delegate lineCheatsClassCellClickDownload:self withStatus:self.status];
    }
    
    
}
@end
